package com.ttdc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ttdc.pojo.Cuisine;
import com.ttdc.pojo.CuisineExample;
import com.ttdc.pojo.Dishes;
import com.ttdc.service.CuisineService;


@Controller

public class CuisineController {

	@Autowired
	private CuisineService cuisineService;
	private CuisineExample example;
	

	//删除 
	@RequestMapping("/delete")	
    public String deleteByPrimaryKey(@RequestParam("cxId")  Integer cxId,Model model){
       cuisineService.deleteByPrimaryKey(cxId);
 
       return "redirect:/cxlist";
    }
	
	//增加
	@SuppressWarnings("finally")
	@RequestMapping("/save")
    public String insert(Cuisine record, Model model){
		try {
        cuisineService.insert(record);
        List<Cuisine> list = cuisineService.selectByAll();
        model.addAttribute("list",list);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			return "redirect:/cxlist";
		}
    }
	
	
	//查询  可多條件查詢
	@RequestMapping("/selectById")
	public String selectByPrimaryKey( HttpSession session, Cuisine cuisine, Model model){
		example = new CuisineExample();
		CuisineExample.Criteria c=example.createCriteria();
		if (cuisine == null) {
			   return "/admin/cuisine";
		} else {
			if (cuisine.getCxName() != null) {
				// 根據菜系名称查詢
				System.out.println(cuisine.getCxName());

				c.andCxNameEqualTo(cuisine.getCxName());
				List<Cuisine> list =cuisineService.selectByExample(example);
				model.addAttribute("list", list);
			}
			
			 return "/admin/cuisine";
		}
			
	}
	@RequestMapping("/selectBycxId")
	public String selectByPrimaryKey( HttpSession session, Integer cxId, Model model){
	          Cuisine cuisine=new Cuisine();
			if (cuisine.getCxId() != null) {
				// 根據訂單編號查詢
				System.out.println(cuisine.getCxId());
				Cuisine list = cuisineService.selectByPrimaryKey(cuisine.getCxId());
				
				 session.setAttribute("list", list);
			}
			 return "/admin/cuisine";
			
	}
	
	//跳转到修改页面，根据id查出来在update那个页面显示
	@RequestMapping("/toupdate")
	public String updateByPrimaryKey(Integer cxId,Model model){
		Cuisine cx =cuisineService.selectByPrimaryKey(cxId);
		 model.addAttribute("cx",cx);
		 return "/admin/updateCuisine";
		
	}
	//由update修改页面放回结果进行修改，然后返回列表
	@RequestMapping("/update")
	public String updateByPrimaryKeySelective( Cuisine record){
		cuisineService.updateByPrimaryKey(record);
	       return "redirect:/cxlist";
	}
	
	// 用条件查询所有的菜系信息
	@RequestMapping("/selectAll")
	public String selectAll(Model model) {
		example = new CuisineExample();
		CuisineExample.Criteria c=example.createCriteria();
		c.andCxIdIsNotNull();
		List<Cuisine> list =cuisineService.selectByExample(example);
		model.addAttribute("list", list);
		 return "/admin/cuisine";
	}
	
	
	//查询所有数据
    @RequestMapping("/cxlist")
   
    public String selectByAll(HttpSession session, Model model){
    	List<Cuisine> list = cuisineService.selectByAll();
         model.addAttribute("list",list);
         session.setAttribute("list", list);
         return "/admin/cuisine";
    }
    
//    public HashMap<Cuisine, List<Dishes>> s() {
//
//		System.out.println("map");
//		HashMap<Cuisine, List<Dishes>> rs = new HashMap<>();
//
//		//
//		List<Cuisine> l1 = cuisineService.selectByAll();
//		//
//		List<Dishes> l2 = selectAllDishes();
//
//		for (Dishes d1 : l2) {
//
//			Cuisine cx = l1.get(d1.getCxId());
//			List<Dishes> l3;
//			if (rs.containsKey(cx)) {
//				l3 = rs.get(cx);
//			} else {
//				l3 = new ArrayList<>();
//			}
//			l3.add(d1);
//		}
//
//		return rs;
//	}

//	@RequestMapping("/s")
//	public String s(Model model) {
//
//		List<Cuisine> rs = cuisineService.selectcxAndDishes();
//
//		// HashMap<Cuisine,List<Dishes>> rs=new HashMap<>();
//		// rs=s();
//		List<Dishes> list=null;
//		
////		System.out.println(rs);
//		for (int i = 0; i < rs.size(); i++) {
////			System.out.println("cpname");
////			System.out.println(rs.get(i).getDishes().getCpName());
//			list.add(rs.get(i).getDishes());
//		}
//		System.out.println(rs);
//		model.addAttribute("dishes",list);
//		model.addAttribute("cx", rs);
//		return "dayin";
//	}
//	@RequestMapping("/ss")
//	public String ss(Integer cxId,Model model) {
//
//		//测试取值cxId
//		System.out.println(cxId);
//		
//		List<Cuisine> rs = cuisineService.selectcxAndDishes();
//
//		System.out.println(rs);
//		// HashMap<Cuisine,List<Dishes>> rs=new HashMap<>();
//		// rs=s();
//		List<Dishes> list=null;
//		
////		System.out.println(rs);
//		for (int i = 0; i < rs.size(); i++) {
////			System.out.println("cpname");
////			System.out.println(rs.get(i).getDishes().getCpName());
//			if(cxId==rs.get(i).getDishes().getCxId()){
//				list.add(rs.get(i).getDishes());
//			}
//		}
//		System.out.println(list);
//		model.addAttribute("dishes",list);
//		model.addAttribute("cx", rs);
//		return "dayin";
//	}

    
    

}